package com.yunxi.module.system.api.dict;

import com.yunxi.framework.common.pojo.CommonResult;
import com.yunxi.framework.common.util.object.BeanUtils;
import com.yunxi.module.system.dto.dict.DictDataRespDTO;
import com.yunxi.module.system.model.dict.DictDataDO;
import com.yunxi.module.system.service.dict.DictDataService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 提供 RESTful API 接口，给 Feign 调用
 * </p>
 *
 * @author lidy
 * @since 2024-05-27
 */
@RestController
@Validated
public class DictDataApiImpl implements DictDataApi {

    @Resource
    private DictDataService dictDataService;

    /**
     * 校验字典是否有效
     *
     * @param dictType 字典类型
     * @param values   字典值
     * @return 结果
     */
    @Override
    public CommonResult<Boolean> validateDictDataList(String dictType, Collection<String> values) {
        dictDataService.validateDictDataList(dictType, values);
        return CommonResult.success(true);
    }

    /**
     * 获得指定字典类型的字典数据列表
     *
     * @param dictType 字典类型
     * @return 字典值
     */
    @Override
    public CommonResult<List<DictDataRespDTO>> getDictDataList(String dictType) {
        List<DictDataDO> dictDataDOS = dictDataService.getDictDataListByDictType(dictType);
        return CommonResult.success(BeanUtils.toBean(dictDataDOS, DictDataRespDTO.class));
    }

    /**
     * 获得指定的字典数据
     *
     * @param dictType 字典类型
     * @param value    字典值
     * @return 字典信息
     */
    @Override
    public CommonResult<DictDataRespDTO> getDictData(String dictType, String value) {
        DictDataDO dictDataDO = dictDataService.getDictData(dictType, value);

        return CommonResult.success(BeanUtils.toBean(dictDataDO, DictDataRespDTO.class));
    }

    /**
     * 解析获得指定的字典数据
     *
     * @param dictType 字典类型
     * @param label    字典标签
     * @return 字典信息
     */
    @Override
    public CommonResult<DictDataRespDTO> parseDictData(String dictType, String label) {
        DictDataDO dictDataDO = dictDataService.parseDictData(dictType, label);
        return CommonResult.success(BeanUtils.toBean(dictDataDO, DictDataRespDTO.class));
    }
}
